home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / manual / examples / inetsrv.c < prev    next >
C/C++ Source or Header  |  1994-02-13  |  2KB  |  102 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netdb.h>
  9.  
  10. #define PORT    5555
  11. #define MAXMSG    512
  12.  
  13. int
  14. read_from_client (int filedes)
  15. {
  16.   char buffer[MAXMSG];
  17.   int nbytes;
  18.  
  19.   nbytes = read (filedes, buffer, MAXMSG);
  20.   if (nbytes < 0)
  21.     {
  22.       /* Read error. */
  23.       perror ("read");
  24.       exit (EXIT_FAILURE);
  25.     }
  26.   else if (nbytes == 0)
  27.     /* End-of-file. */
  28.     return -1;
  29.   else
  30.     {
  31.       /* Data read. */
  32.       fprintf (stderr, "Server: got message: `%s'\n", buffer);
  33.       return 0;
  34.     }
  35. }
  36.  
  37. int
  38. main (void)
  39. {
  40.   extern int make_socket (unsigned short int port);
  41.   int sock;
  42.   fd_set active_fd_set, read_fd_set;
  43.   int i;
  44.   struct sockaddr_in clientname;
  45.   size_t size;
  46.  
  47.   /* Create the socket and set it up to accept connections. */
  48.   sock = make_socket (PORT);
  49.   if (listen (sock, 1) < 0)
  50.     {
  51.       perror ("listen");
  52.       exit (EXIT_FAILURE);
  53.     }
  54.  
  55.   /* Initialize the set of active sockets. */
  56.   FD_ZERO (&active_fd_set);
  57.   FD_SET (sock, &active_fd_set);
  58.  
  59.   while (1)
  60.     {
  61.       /* Block until input arrives on one or more active sockets. */
  62.       read_fd_set = active_fd_set;
  63.       if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0)
  64.     {
  65.       perror ("select");
  66.       exit (EXIT_FAILURE);
  67.     }
  68.  
  69.       /* Service all the sockets with input pending. */
  70.       for (i = 0; i < FD_SETSIZE; ++i)
  71.     if (FD_ISSET (i, &read_fd_set))
  72.       {
  73.         if (i == sock)
  74.           {
  75.         /* Connection request on original socket. */
  76.         int new;
  77.         size = sizeof (clientname);
  78.         new = accept (sock, (struct sockaddr *) &clientname, &size);
  79.         if (new < 0)
  80.           {
  81.             perror ("accept");
  82.             exit (EXIT_FAILURE);
  83.           }
  84.         fprintf (stderr,
  85.              "Server: connect from host %s, port %hd.\n",
  86.              inet_ntoa (clientname.sin_addr),
  87.              ntohs (clientname.sin_port));
  88.         FD_SET (new, &active_fd_set);
  89.           }
  90.         else
  91.           {
  92.         /* Data arriving on an already-connected socket. */
  93.         if (read_from_client (i) < 0)
  94.           {
  95.             close (i);
  96.             FD_CLR (i, &active_fd_set);
  97.           }
  98.           }
  99.       }
  100.     }
  101. }
  102.